package com.jiantong.work.impl.manager;

import com.google.common.collect.Maps;
import com.jiantong.common.util.UserUtil;
import com.jiantong.user.ext.LoginUser;
import com.jiantong.work.enums.DocumentsType;
import com.jiantong.work.impl.dao.PurchaseRecordDao;
import com.jiantong.work.impl.dao.ReportHandleResumeDao;
import com.jiantong.work.model.PurchaseRecord;
import com.jiantong.work.model.ReportHandleResume;
import com.jiantong.work.util.Utils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Map;

/**
 * Created maowenjie on 2017/4/6.
 * 作废
 */
@Component
public class PurchaseRecordManager {
    private final PurchaseRecordDao purchaseRecordDao;
    private final ReportHandleResumeDao reportHandleResumeDao;

    @Autowired
    public PurchaseRecordManager(PurchaseRecordDao purchaseRecordDao, ReportHandleResumeDao reportHandleResumeDao) {
        this.purchaseRecordDao = purchaseRecordDao;
        this.reportHandleResumeDao = reportHandleResumeDao;
    }
    public Map<String,Object>createPurchaseRecord(PurchaseRecord purchaseRecord, Integer toStatus){
        Integer fromStatus = purchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Map<String,Object>map = Maps.newHashMap();

        if(toStatus != null && !fromStatus.equals(toStatus)){
            purchaseRecord.setStatus(toStatus);
            purchaseRecordDao.create(purchaseRecord);
            map.put("purchaseRecordId",purchaseRecord.getId());

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(purchaseRecord.getId());
            reportHandleResume.setReportNumber(purchaseRecord.getNumber());
            reportHandleResume.setType(DocumentsType.QUARANTINE_CENTER_HARMLESS.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
//            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
//            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            purchaseRecordDao.create(purchaseRecord);
            map.put("purchaseRecordId",purchaseRecord.getId());
        }
        return map;
    }

    @Transactional
    public Map<String , Object> updatePurchaseRecord(PurchaseRecord purchaseRecord,Integer toStatus){
        Integer fromStatus = purchaseRecord.getStatus();
        ReportHandleResume reportHandleResume;
        Boolean flagPurchaseRecord;
        Map<String , Object> map = Maps.newHashMap();
        if(toStatus != null && !fromStatus.equals(toStatus)){
            purchaseRecord.setStatus(toStatus);
            flagPurchaseRecord = purchaseRecordDao.update(purchaseRecord);
            map.put("flagPurchaseRecord",flagPurchaseRecord);

            reportHandleResume = new ReportHandleResume();
            reportHandleResume.setReportId(purchaseRecord.getId());
            reportHandleResume.setReportNumber(purchaseRecord.getNumber());
            reportHandleResume.setType(DocumentsType.QUARANTINE_CENTER_HARMLESS.value());
            reportHandleResume.setFromStatus(fromStatus);
            reportHandleResume.setToStatus(toStatus);
//            reportHandleResume.setUserId(UserUtil.getCurrentUser().getId());
//            reportHandleResume.setUserName(UserUtil.getCurrentUser().getName());
            reportHandleResumeDao.create(reportHandleResume);
            map.put("reportHandleResumeId",reportHandleResume.getId());

        }else {
            flagPurchaseRecord = purchaseRecordDao.update(purchaseRecord);
            map.put("flagPurchaseRecord",flagPurchaseRecord);
        }
        return map;
    }
}
